package hhm.android.base.activity;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.trello.rxlifecycle4.components.support.RxAppCompatActivity;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import hhm.android.base.fragment.LoadingFragment;
import hhm.android.library.bus.Bus;

import static hhm.android.base.activity.DeviceUtils.SYS_FLYME;
import static hhm.android.base.activity.DeviceUtils.SYS_MIUI;

/**
 * Created by test on 2017/9/29.
 * keyboard base
 */

public class BaseActivity extends RxAppCompatActivity {

    public static float fontScale = 1.0f;
    //    public static int themeColor = 5;
    public static int themeColor = 0xFFA7E5F7;//首页颜色
    //    public static int themeColor = 0xFFAEE0F4;//邀请页颜色
    protected String system;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(new BaseActivityLifeCycle());
        system = DeviceUtils.getSystem();
        Bus.getDefault().register(this);

    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = res.getConfiguration();
        config.fontScale = fontScale;
//        config.setLocale(new Locale(AppApplication.language));
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    protected void setThemeColor(int color) {
        themeColor = color;
    }

    protected void setTitleWhiteAndTextBlank() {
        if (SYS_MIUI.equals(system)) {//小米
            //设置状态栏随布局变化 文字黑色
            setMyTitleColor(Color.WHITE, false, false);

            MIUISetStatusBarLightMode(getWindow(), true);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        } else if (SYS_FLYME.equals(system)) {//魅族
            //设置状态栏随布局变化 文字黑色
            setMyTitleColor(Color.WHITE, false, false);

            FlymeSetStatusBarLightMode(getWindow(), true);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //设置状态栏随布局变化 文字黑色
            setMyTitleColor(Color.WHITE, false, false);

            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
//        if (themeColor == 1) {
//            setTheme(R.style.black_theme);
//        } else if (themeColor == 2) {
//            setTheme(R.style.light_blue_theme);
//        } else if (themeColor == 3) {
//            setTheme(R.style.red_theme);
//        } else if (themeColor == 4) {
//            setTheme(R.style.deep_blue_theme);
//        } else if (themeColor == 5) {
//            setTheme(R.style.standard_theme);
//        } else if (themeColor == 6) {
//            setTheme(R.style.pink_theme);
//        }
    }

    public void setTitleMainColorAndTextBlank() {
        setMyTitleColor(false);
    }

    public void setTitleMainColorAndTextWhite() {
        setMyTitleColor(true);
    }

    public void setTitleBlankAndTextWhite() {
        setMyTitleColor(Color.BLACK, true);
    }

//    public void setTitleWhiteAndTextBlank() {
//        setMyTitleColor(Color.WHITE, false);
//    }

    public void setMyTitleColor(boolean whiteText) {
        setMyTitleColor(themeColor, false, whiteText);
    }

    public void setMyTitleColor(int color, boolean whiteText) {
        setMyTitleColor(color, false, whiteText);
    }

    /**
     * 沉浸式状态栏
     */
    public void setMyTitleColor(int intColor, boolean isStatusColorTransform, boolean whiteText) {
//        if (intColor == 0) {
//            //透明色 暂不作处理
//        } else if (intColor == 1) {
//            intColor = 0xff232323;
//            setTheme(R.style.black_theme);
//        } else if (intColor == 2) {
//            intColor = 0xff3986ff;
//            setTheme(R.style.light_blue_theme);
//        } else if (intColor == 3) {
//            intColor = 0xffb51818;
//            setTheme(R.style.red_theme);
//        } else if (intColor == 4) {
//            intColor = 0xff5a79ff;
//            setTheme(R.style.deep_blue_theme);
//        } else if (intColor == 5) {
//            intColor = 0xff2702bb;
//            setTheme(R.style.standard_theme);
//        } else if (intColor == 6) {
//            intColor = 0xff7345ef;
//            setTheme(R.style.pink_theme);
//        }

        if (isStatusColorTransform) {
            intColor = 0;
        }
        View decorView = getWindow().getDecorView();
        int options;
        //状态栏文字白色
        if (whiteText) {
            options = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        } else {
            //状态栏文字黑色
            options = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
        }

        decorView.setSystemUiVisibility(options);
        getWindow().setStatusBarColor(intColor);
    }

    /**
     * 设置状态栏图标为深色和魅族特定的文字风格
     * 可以用来判断是否为Flyme用户
     *
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * 设置状态栏字体图标为深色，需要MIUIV6以上
     *
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("addExtraFlags", int.class, int.class);
                if (dark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    /*
     * 全屏幕专用
     */
    public void splashTheme() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Bus.getDefault().unregister(this);
    }

    public void showToast(String s) {
        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void finish() {
        super.finish();
        Bus.getDefault().unregister(this);
    }

    public int dp2px(Float dpValue) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getResources().getDisplayMetrics()));
    }

    public void showLoadingFragment(int viewId) {
        LoadingFragment mLoadingFragment = LoadingFragment.newInstance();
        getSupportFragmentManager()
                .beginTransaction()
                .replace(viewId, mLoadingFragment, LoadingFragment.TAG)
                .commitAllowingStateLoss();
    }

    public void removeLoadingFragment() {
        LoadingFragment mLoadingFragment = findLoadingFragment();
        if (mLoadingFragment != null) {
            mLoadingFragment.removeSelf(getSupportFragmentManager());
        }
    }

    public void showNoDataView() {
        LoadingFragment mLoadingFragment = findLoadingFragment();
        if (mLoadingFragment != null) {
            mLoadingFragment.showNoDataView();
        }

    }

    public LoadingFragment findLoadingFragment() {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(LoadingFragment.TAG);
        if (fragment != null) {
            return (LoadingFragment) fragment;
        }
        return null;
    }
}